package com.geo.keyword.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geo.keyword.entity.KeywordAnalysisResult;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 关键词分析结果Mapper接口
 */
@Mapper
public interface KeywordAnalysisResultMapper extends BaseMapper<KeywordAnalysisResult> {
    
    /**
     * 根据关键词ID查询最新分析结果
     */
    @Select("SELECT * FROM keyword_analysis_results WHERE keyword_id = #{keywordId} ORDER BY created_at DESC LIMIT 1")
    KeywordAnalysisResult findLatestByKeywordId(@Param("keywordId") String keywordId);
    
    /**
     * 根据用户ID查询分析结果
     */
    @Select("SELECT * FROM keyword_analysis_results WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<KeywordAnalysisResult> findByUserId(@Param("userId") String userId);
    
    /**
     * 根据时间范围查询分析结果
     */
    @Select("SELECT * FROM keyword_analysis_results WHERE created_at >= #{startTime} AND created_at <= #{endTime}")
    List<KeywordAnalysisResult> findByTimeRange(@Param("startTime") LocalDateTime startTime, 
                                               @Param("endTime") LocalDateTime endTime);
    
    /**
     * 根据分析类型查询结果
     */
    @Select("SELECT * FROM keyword_analysis_results WHERE analysis_type = #{analysisType}")
    List<KeywordAnalysisResult> findByAnalysisType(@Param("analysisType") String analysisType);
    
    /**
     * 查询待处理的分析任务
     */
    @Select("SELECT * FROM keyword_analysis_results WHERE status = 'PENDING' ORDER BY created_at ASC")
    List<KeywordAnalysisResult> findPendingTasks();
}